package com.carlinfo.stu_ques.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.common.util.DateUtil;
import com.carlinfo.stu_ques.demo.pojo.Dynasty;
import com.carlinfo.stu_ques.demo.pojo.HuLuWa;
import com.carlinfo.stu_ques.demo.service.IDemoService;
import com.carlinfo.stu_ques.demo.service.impl.DemoServiceImpl;

public class ClientMain
{
	private DateUtil dateUtil = new DateUtil();
	private IDemoService demoService  = new DemoServiceImpl();
	public static void main(String[] args)
	{
		ClientMain clientMain = new ClientMain();
		clientMain.lanuch();
	}
	private void lanuch()
	{
		System.out.println("欢迎使用本系统，按下h（help）可以查看帮助");
		BufferedReader br =null;
		try
		{
			InputStream in =  System.in;
			InputStreamReader isr = new InputStreamReader(in,"UTF-8");
			br = new BufferedReader(isr);
			String line = "";
			while(true)
			{
				System.out.println("==主菜单如下：==按n停止（不区分大小写）；按下h（help）可以查看帮助");
				line = br.readLine();	
				line = 	line.trim();
				if ("n".equalsIgnoreCase(line))
				{
					System.out.println("欢迎下次再玩");
					break;
				}else if ("h".equalsIgnoreCase(line))
				{
						help();
				}else if ("1".equalsIgnoreCase(line))
				{
					selectListDynastyService();
				}else if ("5".equalsIgnoreCase(line))
				{
					this.selectOneDynastyService(br);
				}else if ("2".equalsIgnoreCase(line))
				{
					this.saveOneDynastyService(br);
				}else if ("3".equalsIgnoreCase(line))
				{
					this.updateOneDynastyService(br);
				}else if ("4".equalsIgnoreCase(line))
				{
					this.deleteOneDynastyService(br);
				}else if ("6".equalsIgnoreCase(line))
				{
					this.selectListHuLuWaService();
				}else if ("7".equalsIgnoreCase(line))
				{
					this.saveOneHuLuWaService(br);
				}else if ("8".equalsIgnoreCase(line))
				{
					this.updateOneHuLuWaService(br);
				}else if ("9".equalsIgnoreCase(line))
				{
					this.deleteOneHuLuWaService(br);
				}else if ("10".equalsIgnoreCase(line))
				{
					this.selectOneHuLuWaService(br);
				}
				
			}
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				if (br!=null)
				{
					br.close();
					br = null;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private HuLuWa selectOneHuLuWaService(BufferedReader br)
	{
		System.out.println("=查询单个葫芦娃，请输入id=");
		try
		{
			String line = br.readLine();
			line = line.trim();
			Map<String,Object> condMap = new HashMap<String,Object>();
			condMap.put("id", Integer.valueOf(line));
			HuLuWa huluwa = this.demoService.selectOneHuLuWaService(condMap);
			if (huluwa == null)
			{
				System.out.println("你输入的id有误，找不到");
			}else
			{
				String title = this.demoService.printTitleHuLuWaService();
				System.out.println(title);
				System.out.println(huluwa);
			}
			return huluwa;
		} catch (NumberFormatException e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("==您输入的字符串变不成数字==");
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	private void deleteOneHuLuWaService(BufferedReader br)
	{
		HuLuWa huluwa = this.selectOneHuLuWaService(br);
		if (huluwa!=null)
		{
			this.demoService.deleOneHuLuWaService(huluwa);
			System.out.println("删除成功，id："+huluwa.getId());
		}else 
		{
			System.out.println("此条记录不存在");
		}
	}
	private void updateOneHuLuWaService(BufferedReader br)
	{
		HuLuWa huluwa = this.selectOneHuLuWaService(br);
		if (huluwa == null)
		{
			return;
		}
		String templateStr = "更新的示例："+huluwa.getName()+"\\|"+huluwa.getIdDynasty()+"\\|"+huluwa.getAge();
		System.out.println(templateStr);
		try
		{
			String line = br.readLine();
			line = line.trim();
			String [] lines = line.split("\\|");
			if (lines.length!=3)
			{
				System.out.println("输入的格式不正确");
				return	;
			}
			huluwa.setName(lines[0]);
			huluwa.setAge(Integer.valueOf(lines[2]));
			huluwa.setIdDynasty(Integer.valueOf(lines[1]));
			this.demoService.updateOneHuLuWaService(huluwa);
			System.out.println("更新成功,id:"+huluwa.getId());
		} catch (NumberFormatException e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("输入的格式不正确");
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void saveOneHuLuWaService(BufferedReader br)
	{
		System.out.println("=请输入葫芦娃的相关信息：（名字|朝代编号|年龄）=");
		System.out.println("示例:大娃|2|6");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String [] lines = line.split("\\|");
			if (lines.length!=3)
			{
				System.out.println("输入有误");
				return ;
			}
			HuLuWa huluwa = new HuLuWa();
			huluwa.setName(lines[0]);
			huluwa.setAge(Integer.valueOf(lines[2]));
			huluwa.setIdDynasty(Integer.valueOf(lines[1]));
			huluwa.setCreateTime(new Date());
			huluwa.setUpdateTime(new Date());
			int id = this.demoService.saveOneHuLuWaService(huluwa);
			System.out.println("保存成功，id："+id);
			
		} catch (NumberFormatException e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("你输入的字符串不是整数");
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void selectListHuLuWaService()
	{
		List<HuLuWa> huluwaList = this.demoService.selectListHuLuWaService();
		String title = this.demoService.printTitleHuLuWaService();
		System.out.println(title);
		for (HuLuWa huluwa : huluwaList)
		{
			Dynasty dynasty = huluwa.getDynasty();
			String dynaName = "无";
			if (dynasty!=null)
			{
				dynaName = dynasty.getName();
			}
			System.out.println(huluwa.getId() + "\t"+dynaName + "\t"+ huluwa.getName() +
					"\t"+ huluwa.getIdDynasty()+"\t" +huluwa.getAge() +"\t"+ 
					"\t"+ huluwa.getCreateTime().toLocaleString() +"\t" + huluwa.getUpdateTime().toLocaleString());
		}
		
	}		
	private void deleteOneDynastyService(BufferedReader br)
	{
		Dynasty dynasty = this.selectOneDynastyService(br);
		if (dynasty!=null)
		{
			this.demoService.deleteOneDynastyService(dynasty);
			System.out.println("删除成功，id："+dynasty.getId());
		}else
		{
			System.out.println("此条记录不存在");
		}
	}
	private void updateOneDynastyService(BufferedReader br)
	{
		Dynasty dynasty = this.selectOneDynastyService(br);
		if(dynasty == null)
		{
			return;
		}
		String templateStr = "更新的示例："+dynasty.getName()+"|"+dynasty.getAge()+"|"+dynasty.getCapital()
		+"|"+this.dateUtil.dateToStr(dynasty.getSt());
		System.out.println(templateStr);
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if(lines.length!=4)
			{
				System.out.println("输入的格式不正确");
				return;
			}
			Date stDate = dateUtil.strToDate(lines[3]);
			dynasty.setName(lines[0]);
			dynasty.setAge(Short.valueOf(lines[1]));
			dynasty.setCapital(lines[2]);
			dynasty.setSt(stDate);
			dynasty.setUpdateTime(new Date());
			this.demoService.updateOneDynastyService(dynasty);
			System.out.println("更新成功，id："+dynasty.getId());
		} catch (NumberFormatException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void saveOneDynastyService(BufferedReader br)
	{
		System.out.println("=请输入朝代的相关信息：（名字|年龄|首都|开国时间）=");
		System.out.println("示例:明朝|279|北京|1368-01-01");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if(lines.length!=4)
			{
				System.out.println("输入的格式不正确");
				return;
			}
			Date stDate = dateUtil.strToDate(lines[3]);
			Dynasty dynasty = new Dynasty();
			dynasty.setName(lines[0]);
			dynasty.setAge(Short.valueOf(lines[1]));
			dynasty.setCapital(lines[2]);
			dynasty.setSt(stDate);
			dynasty.setCreateTime(new Date());
			dynasty.setUpdateTime(new Date());
			int id  = this.demoService.saveOneDynastyService(dynasty);
			System.out.println("==保存成功,id:==" + id);
		} catch (NumberFormatException e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("==您输入的字符串变不成数字==");
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private Dynasty selectOneDynastyService(BufferedReader br)
	{
		System.out.println("=查询单个朝代，请输入id=");
		try
		{
			String id = br.readLine();
			Map<String,Object> condMap = new HashMap<String,Object>();
			condMap.put("id", Integer.valueOf(id));
			Dynasty dynasty = this.demoService.selectOneDynastyService(condMap);
			if (dynasty == null)
			{
				System.out.println("你输入的ID有误，找不到朝代");
			}else
			{
				String title = this.demoService.printTitleDynastyService() ; 
				System.out.println(title);
				System.out.println(dynasty);

			}
			return dynasty;
		} catch (NumberFormatException e)
		{
			// TODO Auto-generated catch block
			//.printStackTrace();
			System.out.println("你输入的id应该为整数");
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	private void selectListDynastyService()
	{
		List<Dynasty> dynastyList = this.demoService.selectListDynastyService();
				//System.out.println("id\t名字\t首都\t年龄\t开国时间\t\t\t创建时间\t\t\t更新时间");
			String title = this.demoService.printTitleDynastyService() ; 
				System.out.println(title);
				/* 循环List,
				 * 三种List循环,都可以 */
				for (Dynasty dynasty : dynastyList)
				{
					System.out.println(dynasty.getId() + "\t"+ dynasty.getName() +
						"\t"+ dynasty.getCapital() +"\t"+ dynasty.getAge() +"\t"+ 
						dynasty.getSt().toLocaleString() +
						"\t"+ dynasty.getCreateTime().toLocaleString() +"\t" + dynasty.getUpdateTime().toLocaleString());
				}

	}
	private void help()
	{	
				/*1:查询所有朝代
		 		 * 2:添加朝代
				 * 3:修改朝代;(先输入朝代id,填写要修改的内容)
				 * 4:删除朝代
				 * 5:查询所有皇上
				 * 6:添加皇上
				 * 7:修改皇上;(先输入皇上id,填写要修改的内容)
				 * 8:删除皇上*/
				System.out.println("1:查询所有朝代");
				System.out.println("2:添加朝代");
				System.out.println("3:修改朝代;(先输入朝代id,填写要修改的内容)");
				System.out.println("4:删除朝代");
				System.out.println("5:查看单个朝代");
				System.out.println("6:查询所有皇上");
				System.out.println("7:添加皇上");
				System.out.println("8:修改皇上;(先输入皇上id,填写要修改的内容)");
				System.out.println("9:删除皇上");
				System.out.println("10:查询单个皇上");
	
	}
}
